import { defineStore } from 'pinia'
import { getCheckInfoAPI, submitOrderAPI } from '@/apis/checkout';
import { ref } from 'vue'

import { useRouter } from 'vue-router';
export const useCheckoutStore = defineStore('checkout', () => {
	const checkInfo = ref({})  // 订单对象

	const router = useRouter()
	// 配送时间
	const deliveryTimeType = ref(1)

	// 支付类型
	const payType = ref(1)

	// 获取订单信息
	const getCheckInfo = async () => {
		const res = await getCheckInfoAPI().then(res => res.data);
		checkInfo.value = res.result;
	}

	// 创造订单
	const submit = async (data,) => {
		const res = await submitOrderAPI(data).then(res => res.data)
		const orderId = res.result.id
		router.push({
			path: '/pay',
			query: {
				id: orderId
			}
		})
	}
	return {
		deliveryTimeType,
		payType,
		checkInfo,
		getCheckInfo,
		submit
	}
})